package com.xuecheng.media.controller;

import com.xuecheng.api.media.MediaApi;
import com.xuecheng.api.media.model.ResourceUrlModel;
import com.xuecheng.api.media.model.VodUploadRequest;
import com.xuecheng.api.media.model.VodUploadToken;
import com.xuecheng.api.media.model.dto.MediaDTO;
import com.xuecheng.api.media.model.qo.QueryMediaModel;
import com.xuecheng.api.media.model.vo.MediaVO;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.domain.response.RestResponse;
import com.xuecheng.media.common.utils.UAASecurityUtil;
import com.xuecheng.media.convert.MediaConvert;
import com.xuecheng.media.service.MediaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created on 2021/04/03/23:07 周六
 *
 * @author Lantin
 */
@RestController
public class MediaController implements MediaApi {

    @Autowired
    private MediaService mediaService;

    @PostMapping("media/vod-token")
    public VodUploadToken createVODUploadToken(@RequestBody VodUploadRequest vodUploadRequest) {

        return mediaService.createVODUploadToken(vodUploadRequest);
    }

    @GetMapping("media/refresh-vod-token/{videoId}")
    public VodUploadToken refreshVODUploadToken(@PathVariable String videoId) {
        return mediaService.refreshVODUploadToken(videoId);
    }

    @PostMapping("media/list")
    public PageVO queryMediaList(PageRequestParams params,
                                 @RequestBody QueryMediaModel model) {

        Long companyId = UAASecurityUtil.getCompanyId();

        return mediaService.queryMediaList(params, model, companyId);
    }

    @PostMapping("media")
    public MediaDTO createMedia(@RequestBody MediaVO mediaVO) {

        Long companyId = UAASecurityUtil.getCompanyId();

        MediaDTO mediaDTO = MediaConvert.INSTANCE.mediaVoToDTO(mediaVO);

        return mediaService.createMedia(mediaDTO, companyId);
    }


    @GetMapping("media/preview/{mediaId}")
    public String previewMedia(@PathVariable Long mediaId) {
        Long companyId = UAASecurityUtil.getCompanyId();
        return mediaService.previewMedia(mediaId, companyId);
    }

    @GetMapping("l/media/{mediaId}")
    public RestResponse getByMediaId(@PathVariable Long mediaId) {

        return mediaService.getByMediaId(mediaId);
    }

    @DeleteMapping("media/{mediaId}")
    public boolean deleteMedia(@PathVariable Long mediaId) {

        Long companyId = UAASecurityUtil.getCompanyId();
        return mediaService.deleteMedia(mediaId, companyId);
    }
    @GetMapping("l/media-url/{mediaId}")
    public RestResponse<ResourceUrlModel> getMediaUrl(@PathVariable Long mediaId) {

        return mediaService.getMediaUrl(mediaId);
    }




}
